package com.itheima.app.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.SmsTemplate;
import com.itheima.domain.db.*;
import com.itheima.domain.vo.ErrorResult;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.SettingVo;
import com.itheima.service.db.*;
import com.itheima.util.ConstantUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import sun.security.util.AuthResources_it;

import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 请不要叫我高司令
 * @Date 2022/3/14 23:41
 * @Version 1.0
 */

@Component
public class SettingManager {


    @DubboReference
    private QuestionService questionService;

    @DubboReference
    private NotificationService notificationService;

    @Autowired
    private SmsTemplate smsTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @DubboReference
    private UserService userService;

    @DubboReference
    private BlackListService blackListService;

    @DubboReference
    private UserInfoService userInfoService;




    // 查询通用设置概要
    public ResponseEntity settings() {

        User user = UserHolder.get();

        Question question = questionService.findByUserId(user.getId());

        Notification notification = notificationService.findByUserId(user.getId());

        SettingVo vo = new SettingVo();
        vo.setId(user.getId());

        if (ObjectUtil.isNotEmpty(question)) {

            vo.setStrangerQuestion(question.getStrangerQuestion());

        }
        if (ObjectUtil.isNotEmpty(notification)) {

            vo.setGonggaoNotification(notification.getGonggaoNotification());
            vo.setPinglunNotification(notification.getPinglunNotification());
            vo.setLikeNotification(notification.getLikeNotification());

        }

        vo.setPhone(user.getMobile());//手机号

        return ResponseEntity.ok(vo);


    }


    // 发送验证码
    public void sendVerificationCode() {

        User user = UserHolder.get();
        //1.生成验证码
        String smsCode = RandomUtil.randomNumbers(6);
        smsCode = "234567";

        //调用阿里云发送短信
        //smsTemplate.sendSms(phone, smsCode);//TODO 以后再改

        //3.redis存验证码,存五分钟
        stringRedisTemplate.opsForValue().set(ConstantUtil.SMS_CODE + user.getMobile(), smsCode, Duration.ofMinutes(5));
    }




    //校验验证码
    public ResponseEntity checkVerificationCode(String verificationCode) {


        User user = UserHolder.get();
        String phone = user.getMobile();


        //1.取出验证码
        String redisCode = stringRedisTemplate.opsForValue().get(ConstantUtil.SMS_CODE + phone);

        //2.比对验证码
        if (!StrUtil.equals(verificationCode, redisCode)) {
            return ResponseEntity.status(500).body(ErrorResult.checkVerificationCodeError());

        }
        //封装结果
        Map resultMap = new HashMap();
        resultMap.put("verification", true);
        return ResponseEntity.ok(resultMap);


    }

    //设置手机号
    public void updatePhone(String phone) {
        //获取旧手机号
        User user = UserHolder.get();
        String oldPhone = user.getMobile();

        //封装新手机号
        user.setMobile(phone);
        userService.update(user);

        if (StringUtils.isNotEmpty(oldPhone)) {
            //删除旧token
            stringRedisTemplate.delete(ConstantUtil.USER_TOKEN + oldPhone);
        }

        //退出登录
        UserHolder.remove();
    }


    //设置陌生人问题
    public void setQuestion(String content) {

        User user = UserHolder.get();
        Question question = questionService.findByUserId(user.getId());
        if (ObjectUtil.isNotEmpty(question)) {

            question.setStrangerQuestion(content);
            questionService.Update(question);

        } else {

            question = new Question();
            question.setUserId(user.getId());
            question.setStrangerQuestion(content);
            questionService.save(question);

        }
    }

    // 设置推送通知
    public void setNotification(Notification notificationParam) {

        User user = UserHolder.get();
        Notification notification = notificationService.findByUserId(user.getId());

        //有就更新
        if (ObjectUtil.isNotEmpty(notification)) {

            notificationParam.setId(notification.getId());
            notificationService.update(notificationParam);

        } else {

            notificationParam.setUserId(user.getId());
            notificationService.save(notificationParam);

        }


    }
    // 黑名单分页查询
    public ResponseEntity findBlackListByPage(Integer pageNum, Integer pageSize) {

        User user = UserHolder.get();
        PageBeanVo pageBeanVo = blackListService.findByPage(user.getId(), pageNum, pageSize);

        List<BlackList> blackLists = (List<BlackList>) pageBeanVo.getItems();
        ArrayList<UserInfo> userInfoArrayList = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(blackLists)) {

            for (BlackList blackList : blackLists) {

                Long blackUserId = blackList.getBlackUserId();

                UserInfo userInfo = userInfoService.findById(blackUserId);
                userInfoArrayList.add(userInfo);


            }

        }
        pageBeanVo.setItems(userInfoArrayList);

        return ResponseEntity.ok(pageBeanVo);


    }
    // 删除黑名单
    public void deleteBlackList(Long blackUserId) {

        User user = UserHolder.get();
        blackListService.delete(user.getId(), blackUserId);

    }
}
